#include <SFML/Graphics.hpp>
#include <iostream>
#include "EditText.h"

using namespace std;
using namespace sf;

EditText::EditText(int x, int y, int longueur) : positionX(x), positionY(y), longueur(longueur), focus(false), placeHolder(false), text(""), placeHolderText(""){};

void EditText::setEvent(Event &event){
    if(event.Type == Event::MouseButtonPressed){
        if(event.MouseButton.X > positionX && event.MouseButton.X < positionX + longueur && event.MouseButton.Y > positionY && event.MouseButton.Y < positionY + 30){
            focus = true;
            if(placeHolder){
                placeHolder = false;
            }
        }else{
            focus = false;
            if(text == "")
                placeHolder = true;
        }
    }else if(event.Type == Event::TextEntered && focus){
        text+= event.Text.Unicode;
        if(event.Text.Unicode == 8)
            text = text.substr(0,text.size()-2);
    }
}

void EditText::setPlaceHolder(string placeHolderText){
    placeHolder = true;
    EditText::placeHolderText = placeHolderText;
}

string EditText::getText(){
    return text;
}

void EditText::drawEditText(RenderWindow &app){

    Shape fond;
    String editText;
    editText.SetPosition(positionX, positionY-5);
    if(!placeHolder){
        editText.SetColor(Color::Black);
        editText.SetText(text);
    }
    else{
        editText.SetText(placeHolderText);
        editText.SetColor(Color(200,200,200));
    }
    if(focus)
        fond = Shape::Rectangle(positionX,positionY,positionX+longueur,positionY+30,Color::White,2,Color::Black);
    else
        fond = Shape::Rectangle(positionX,positionY,positionX+longueur,positionY+30,Color(230,230,230),2,Color::Black);
    app.Draw(fond);
    app.Draw(editText);
}
